//
//  ActivityStatisticsTableViewCell.swift
//  MoodDiary
//
//  Created by OVIX on 2020/12/11.
//

import UIKit

protocol ActivityStatisticsTableViewCellDelegate: class {
    func activityStatisticsTableViewCellShare()
}

class ActivityStatisticsTableViewCell: UITableViewCell {
    
    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var label: UILabel!
    
    var delegate: ActivityStatisticsTableViewCellDelegate?
    
    var isMonth: Bool?
    
    var activityDic: [String: Int]? {
        didSet {
            dealActivityData()
        }
    }
    var activityDicArr: [[String: Int]]?
    
    func dealActivityData() {
        
        //把value取出来 按从大到小排序 然后剩下的内容按照固定顺序排序
        var array: [[String: Int]] = []
        
        if activityDic != nil {
            let sortedDic = activityDic!.sorted(by: {$0.1 > $1.1})
            
            for (key, value) in sortedDic {
                if value > 0 {
                    array.append([key: value])
                }
            }
        }
        
        if array.count == 0 {
            
            var keys: [String] = []
            
            for dict2 in array {
                keys.append(dict2.keys.first ?? "")
            }
            
            let shouldContainKeys = ["工作", "学习", "音乐", "美食", "亲子", "健身", "娱乐", "购物", "电影", "旅行", "游戏", "晚餐", "礼物", "爱情", "关系", "阅读", "追剧", "运动", "吃瓜", "熬夜", "烹饪", "家庭", "休息", "宠物", "整理", "冒险", "度假", "徒步", "省钱", "露营", "生病", "骑行", "逛街", "动漫", "游泳", "变美", "奶茶", "茶点", "打坐", "养生", "出海", "偶像", "洗澡", "其它"]
            
            for key in shouldContainKeys {
                if keys.contains(key) == false {
                    array.append([key: 0])
                }
            }
            
        } else {
            
        }
        
        activityDicArr = array
        
        collectionView.reloadData()
        
    }
    
    var beginEndDay = "" {
        didSet {
            
            if isMonth == false {
                let str = "活动统计 " + beginEndDay
                let attStr = NSMutableAttributedString(string: str)
                attStr.addAttributes([.font: UIFont.systemFont(ofSize: 14.0, weight: .light), .foregroundColor: UIColor.hexColor(hex: "#D4D4D4")], range: NSMakeRange(4, beginEndDay.count + 1))
                label.attributedText = attStr
                return
            }
            
            let str = "活动统计 " + (beginEndDay as NSString).substring(to: 2) + "月"
            let attStr = NSMutableAttributedString(string: str)
            attStr.addAttributes([.font: UIFont.systemFont(ofSize: 14.0, weight: .light), .foregroundColor: UIColor.hexColor(hex: "#D4D4D4")], range: NSMakeRange(5, 2 + 1))
            label.attributedText = attStr
            
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(UINib(nibName: "ActivityStatisticsCollectionViewCell", bundle: .main), forCellWithReuseIdentifier: "ActivityStatisticsCollectionViewCell")
        
        bgView.layer.cornerRadius = 12.0
        bgView.layer.shadowOpacity = 1.0
        bgView.layer.shadowOffset = CGSize(width: 0, height: 0)
        bgView.layer.shadowRadius = 2.0
        bgView.layer.shadowColor = UIColor.init(white: 0.6, alpha: 0.4).cgColor
    }
    
    @IBAction func action(_ sender: Any) {
        delegate?.activityStatisticsTableViewCellShare()
    }
    
}

extension ActivityStatisticsTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let margin = 10.0
        let itemLength = (Double(collectionView.width) - margin * 4.0) / 5.0
        return CGSize(width: itemLength, height: itemLength + 20.0)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 10.0 }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { activityDicArr?.count ?? 0 }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: ActivityStatisticsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ActivityStatisticsCollectionViewCell", for: indexPath) as! ActivityStatisticsCollectionViewCell
        
        cell.dic = activityDicArr?[indexPath.row]
        
        return cell
    }
    
}

